package com.liwei.lwmail.elasticsearch.controller;

import com.liwei.common.exception.BizCodeEnume;
import com.liwei.common.to.es.SkuEsModel;
import com.liwei.common.utils.R;
import com.liwei.lwmail.elasticsearch.service.ElasticProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/search/save")
public class ElasticSaveController {

    @Autowired
    private ElasticProductSaveService saveService;

    /**
     * 上架商品
     */
    @PostMapping("/product")
    public R productUpStatus(@RequestBody List<SkuEsModel> skuEsModels){
        Boolean b = false;
        try {
            b = saveService.productUpStatus(skuEsModels);
        } catch (IOException e) {
            log.error("商品上架错误,{}",e);
            return R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnume.PRODUCT_UP_EXCEPTION.getMsg());
        }
        if (b){
            return R.ok();
        }else {
            return R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnume.PRODUCT_UP_EXCEPTION.getMsg());
        }
    }
}
